package org.myontology.controller.rules;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.myontology.controller.AttributeHolder;
import org.myontology.controller.SesameSession;

public class SimpleRuleEngine {

	private Map<String, List<Rule>> rules = new ConcurrentHashMap<String, List<Rule>>();
	
	public boolean check(String action,AttributeHolder attributes,SesameSession session) {
		List<Rule> list = rules.get(action);
		if (list==null) {
			return true;
		}
		for (Iterator<Rule> it = list.iterator();it.hasNext();) {
			Rule r = it.next();
			if (!r.check(attributes,session))
				return false;
		}
		return true;
	}
	
	public void addRule(String key,Rule rule) {
		List<Rule> list = rules.get(key);
		if (list==null) {
			list = new CopyOnWriteArrayList<Rule>();
			rules.put(key, list);
		}
		list.add(rule);
	}
	
}
